#include <allegro.h>
#include <time.h>
#include <math.h>

typedef struct
{ int x;
  int y;
 
  BITMAP * img_right[3];
  BITMAP * img_left[3];
}Personnage;

typedef struct 
{int x,y;
BITMAP * bonus;

int vie;
}Vies;


typedef struct 
{
int x,y;
BITMAP*image;

}Obstacles;


int collision_bonus(Personnage p, Vies v)
{
 if ((p.x > v.x+v.bonus->w -1)||(p.y > v.y+v.bonus->h -1)||(v.x > p.x + p.img_right[0]->w -1)||(v.y > p.y + p.img_right[0]->h -1)) {return 0;}
else {return 1;}  
}

int collision_obstacle(Personnage p, Obstacles o)
{
 if ((p.x > o.x+o.image->w -1)||(p.y > o.y+o.image->h -1)||(o.x > p.x + p.img_right[0]->w -1)||(o.y > p.y + p.img_right[0]->h -1)) {return 0;}
else {return 1;}  
}


void compter_score(BITMAP *Buffer,Personnage p)
{
int score=100;
textprintf_ex(Buffer, font, 300, 30, makecol(0,0,0),-1, " TEMPS: %f s", (float)clock()/CLOCKS_PER_SEC);

if(p.x > SCREEN_W-50) { 
clear_bitmap(Buffer);	
rectfill(Buffer, 150, 150, 600, 350 , makecol(0,0,255));

textprintf_ex(Buffer, font, 270, 200, makecol(0,0,0),-1, "vous avez mis %f secondes! ", (float)clock()/CLOCKS_PER_SEC);



if ((float)clock()/CLOCKS_PER_SEC<=10) 
{ 
textprintf_ex(Buffer,font,270,230,makecol(0,0,0),-1,"SCORE: %d",score); 
}
else if ((float)clock()/CLOCKS_PER_SEC<=20) 
{score=score-20;
 textprintf_ex(Buffer,font,270,230,makecol(0,0,0),-1,"SCORE: %d",score); 
}
else if ((float)clock()/CLOCKS_PER_SEC<=40) {
score=score-40;
 textprintf_ex(Buffer,font,270,230,makecol(0,0,0),-1,"SCORE: %d",score); }
else if ((float)clock()/CLOCKS_PER_SEC<=60) {
score=score-60;
 textprintf_ex(Buffer,font,270,230,makecol(0,0,0),-1,"SCORE: %d",score); }
else if ((float)clock()/CLOCKS_PER_SEC<=80) { 
score=score-80;
textprintf_ex(Buffer,font,270,230,makecol(0,0,0),-1,"SCORE: %d",score); }
else { score=score-100;
textprintf_ex(Buffer,font,270,230,makecol(0,0,0),-1,"SCORE: %d",score); }


blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
}

textprintf_ex(Buffer,font,730,39.5,makecol(0,0,0),-1," 0");
}


void compter_vie(BITMAP *Buffer,Personnage p,Obstacles o,Vies v)
{

while(v.vie!=0)
{
if(p.y==600){v.vie--;}
if (  collision_obstacle(p,o)==1 ) {v.vie--;}
if (  collision_bonus(p,v)==1 ) {v.vie++;}
}

textprintf_ex(Buffer, font, 270, 170, makecol(255,0,0),-1, "vous avez perdu! ", (float)clock()/CLOCKS_PER_SEC);

compter_score(Buffer,p);

}




int main() 
{

Personnage p;
Vies v;
Obstacles o;

BITMAP *Buffer;
BITMAP *Image;
BITMAP *Background;
int i=0;


p.x=100;
p.y=195;


allegro_init(); // Initialisation de la librairie allegro

if (install_keyboard() == -1) // test clavier
{
allegro_message("Error! %s", allegro_error);
return -1;
}

set_color_depth(16); // nombre de bits

if (set_gfx_mode(GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0) != 0)  // test mode graphique
{
set_gfx_mode(GFX_TEXT, 0, 0, 0, 0); //mode texte
allegro_message("Unable to initialize video mode!\n%s\n", allegro_error);
return 1;
}

 // chargement des images

p.img_right[0]=load_bitmap("./Images/right01.bmp", NULL);
p.img_right[1]=load_bitmap("./Images/right02.bmp", NULL);
p.img_right[2]=load_bitmap("./Images/right03.bmp", NULL);
p.img_left[0]=load_bitmap("./Images/left01.bmp", NULL);
p.img_left[1]=load_bitmap("./Images/left02.bmp", NULL);
p.img_left[2]=load_bitmap("./Images/left03.bmp", NULL); 


o.image=load_bitmap("./Images/image.bmp", NULL);
v.bonus=load_bitmap("./Images/bonus.bmp", NULL);
     

Buffer = create_bitmap(800, 600); // creation du bitmap

Image = load_bitmap("./Images/right01.bmp", NULL);
Background = load_bitmap("./Images/Background.bmp", NULL);

blit(Background, Buffer, 0, 0, 0, 0, Background->w, Background->h);// copier le background dans le buffer

draw_sprite(Buffer, Image, p.x, p.y);// dessiner l'image dans le buffer

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);// copier le buffer dans l'ecran

if (Background == NULL) {
allegro_message("could not load images");
allegro_exit();
}


while(!key[KEY_ESC])
{ 

v.vie=3;

if((key[KEY_RIGHT]) && (p.x < SCREEN_W - p.img_right[0]->w))
{
  p.x=p.x +2; 
  
 for(i=0;i<3;i++)
{
blit(Background,Buffer,0,0,0,0,Background->w,Background->h);

draw_sprite(Buffer, p.img_right[i], p.x, p.y);
compter_score(Buffer,p);
textprintf_ex(Buffer,font,110,39.5,makecol(0,0,0),-1," %d ",v.vie);
blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);
    
   }
}
if ((key[KEY_LEFT]) && (p.x>0))
{
  p.x=p.x-2;

 for(i=0;i<3;i++)
{
blit(Background,Buffer,0,0,0,0,Background->w,Background->h);

draw_sprite(Buffer, p.img_left[i], p.x, p.y);

textprintf_ex(Buffer, font, 300, 30, makecol(0,0,0),-1, " TEMPS: %f s", (float)clock()/CLOCKS_PER_SEC);
textprintf_ex(Buffer,font,730,39.5,makecol(0,0,0),-1," 0");
textprintf_ex(Buffer,font,110,39.5,makecol(0,0,0),-1," %d ",v.vie);

blit(Buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H);   
}
}



}

readkey();

destroy_bitmap(Image);// liberer la mémoire: supprimer l'image
destroy_bitmap(Buffer);

//clear_bitmap(Buffer);

return 0;
}
END_OF_MAIN();